package com.lzy.pattern.singleton.demo8;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @ClassName: Client
 * @Description: 破坏单例设计模式：反射
 * 解决办法：当通过反射方式多次调用构造方法进行创建创建时，直接抛异常。不运行此中操作。
 * @Author: LiuZy
 * @Date: 2022/4/10 16:10
 * @Version: 1.0
 */
public class Client {

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

        Class<Singleton> clazz = Singleton.class;

        Constructor<Singleton> cs = clazz.getDeclaredConstructor();
        cs.setAccessible(true);

        Singleton instance1 = cs.newInstance();

        System.out.println(instance1);
        Singleton instance2 = cs.newInstance();

        System.out.println(instance2);
        System.out.println(instance1 == instance2);


    }
}